<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://cdn.bootcdn.net/ajax/libs/socket.io/4.1.3/socket.io.min.js"></script>
  <title>websocket</title>
  
</head>
<body>
  <h1>Echo test</h1>
  <input type="text" id="sendText">
  <button id="sendBtn">发送</button>
  <ul id="recv"></ul>
  <script>
    var recv = document.getElementById('recv');
    //  var websocket = new WebSocket('ws://echo.websocket.org/');
     var websocket = new WebSocket('ws://localhost:8001/topic1');
      websocket.onopen = function () {
        console.log('websocket open');
        recv.innerHTML += '<li>Connected</li>';
      }

      websocket.onclose = function () {
        console.log('websocket close');
        recv.innerHTML += 'Discconnect';
      }

      websocket.onmessage = function (e) {
        console.log('接收', e.data);
        recv.innerHTML += `<li>${e.data}</li>`;
      }

      document.getElementById('sendBtn').onclick = function () {
        websocket.send(document.getElementById('sendText').value);
        document.getElementById('sendText').value = '';
      };
  </script>
</body>
</html>